package com.captain.special.二叉树.简单;

import com.captain.leetcode.TreeNode;
import com.captain.leetcode.链表.ListNode;

/**
 * Des:
 *
 * @author XL
 * @Date 2022/5/6 22:13
 */
public class 路径总和112 {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        if (root == null) return false;
        if (root.left == null && root.right == null && root.val == targetSum) return true;
        return recur(root, 0, targetSum);
    }
    public boolean recur(TreeNode node, int sum, int target) {
        if (node == null) {
            return false;
        }
        sum = sum + node.val;
        if (sum == target && node.left == null && node.right == null) {
            return true;
        }
        boolean leftVal = recur(node.left, sum, target);
        if (leftVal) {
            return true;
        }
        return recur(node.right, sum, target);
    }
}
